<template>
	<view class="container">
		<view class="content">
			<view class="top">
				<h3>{{title}}</h3>
				<view class="info">
					<view class="left">
						<view>姓名：{{studInfo.realName}}</view>
						<view>性别：{{studInfo.sex}}</view>
						<view>组别：{{groupName}}组</view>
						<view>学校：{{studInfo.school}}</view>
					</view>
					<view class="right">
						<image :src="studInfo.imageUrl" mode="aspectFill"></image>
					</view>
				</view>
			</view>
			<h3>考核项目：</h3>
			<view v-show="flag1" style="text-align: center;margin-bottom: 10px;">
				<text style="font-size: 12px;font-weight: 900;color: red;">{{words}}</text>
				<!-- <p style="font-size: 12px;color: red;font-weight: 900;padding-left: 30px;">请在方框内输入相应的成绩</p>
				<p style="font-size: 12px;color: red;font-weight: 900;padding-left: 30px;">例如：100 / 00:00:00(时:分:秒) / 胜</p> -->
			</view>
			<form @submit="formSubmit">
				<view class="outer" v-for="item in projectsList" :key="item.id">
					<view class="title">{{item.name}}</view>
					<input class="chengji" :name="item.id" @focus="wordsChange(item.gradeType)" />
				</view>
				
				<view style="text-align: center;" @click="look">
					录入注意事项<span style="color: red;">({{noticeList}})</span>
				</view>
				<view class="uni-btn-v">
					<button v-show="!flag" style="width: 30%;text-align: center;margin-top: 20px;" form-type="submit">提交成绩</button>
					<button v-show="flag" style="width: 30%;text-align: center;margin-top: 20px;" @click="fanhui">返回</button>
				</view>
			</form>
			
		</view>
	</view>

</template>

<script>
	import {
		numquery,
		wRecord,
		notice
	} from '../../apis'
	export default {
		data() {
			return {
				flag: false,
				title: '',
				studInfo: {},
				projectsList: [],
				ways: '',
				userId: '',
				competitionId: '',
				noticeList: '',
				gradeType: '',
				groupType: 0,
				projectsId: '',
				result: '',
				projectGradeDtos: [],
				groupName:"",
				words:"",
				flag1:false
			}
		},
		onLoad(option) {
			uni.showLoading({
			    title: '加载中'
			});
			console.log("携带的数据为：" + JSON.stringify(option))
			this.groupType = option.groupType
			if(this.groupType == 1){
				this.groupName = "小学"
			}else if(this.groupType == 2){
				this.groupName = "初中"
			}else if(this.groupType == 3){
				this.groupName = "高中"
			}else if(this.groupType == 4){
				this.groupName = "大学"
			}else{
				this.groupName = "附加"
			}
			console.log("组别为："+this.groupType)
			//查询管理员备注
			notice({
				"type": "3"
			}).then((res) => {
				let [err, data] = res
				console.log("管理员备注为"+JSON.stringify( data.data.data))
				console.log("管理员备注为"+JSON.stringify( data.data.data[0]))
				this.noticeList = data.data.data[0].content
			})
			// console.log("跳转成功")
			// console.log("option："+option)
			this.title = option.title
			this.competitionId = option.competitionId
			numquery({
				"competitionId": option.competitionId,
				"phone": option.phone,
				"groupType":option.groupType
			}).then((res) => {
				let [err, data] = res
				console.log(data.data.data.saasUser) //userId在这里id
				this.userId = data.data.data.saasUser.id
				console.log("用户报名科目为："+data.data.data.projects)
				//获取到用户信息
				this.studInfo = data.data.data.saasUser
				//获取到用户报名的项目信息
				this.projectsList = data.data.data.projects
				console.log("this.projectsList为："+JSON.stringify(this.projectsList) )
				uni.hideLoading();
			})
		},
		methods: {
			wordsChange(words){
				this.flag1 = true
				console.log("类型为："+words)
				if(words == "1"){
					this.words = "请输入分数 例如：100"
				}else if(words == "2"){
					this.words = "请输入分数 例如：00:00(分:秒 英文冒号)"
				}else{
					this.words = "请输入分数 例如：胜/负"
				}
			},
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				//表单对象
				var formdata = e.detail.value
				
				var projectGradeDtos = [];
				for (let key in formdata) {
					//根据值来处理
					console.log(formdata[key])
					if(formdata[key].includes(":")){
						// var hour = formdata[key].split(':')[0];
						var min = formdata[key].split(':')[0];
						var sec = formdata[key].split(':')[1];
						formdata[key] = Number(min * 60) + Number(sec);
						// Number(hour * 3600) + 
					}					
					var project = {
						"groupType": this.groupType,
						"projectId": key,
						"result": formdata[key]
					}
					projectGradeDtos.push(project);
				}
				var obj = {
					"competitionId": this.competitionId,
					"projectGradeDtos": projectGradeDtos,
					"userId": this.userId
				};
				console.log(obj)
				
				wRecord({
					"competitionId": this.competitionId,
					"projectGradeDtos": projectGradeDtos,
					"userId": this.userId
				}).then((res) => {
					let [err, data] = res
					if(data.data.code==200){
						uni.showToast({
							title: '成绩录入成功',
							icon:'success'
						});
						this.flag = true
					}else{
						uni.showToast({
							title: '成绩录入失败',
							image:'../../static/gantan.png'
						});
					}  
				})

			},
			fanhui(){
				uni.switchTab({
					url:'../rank/rank'
				})
			},
			getresult: function(e) {
				console.log("jieguo:" + e.detail.value)
				if (this.gradeType == 2) {
					var time = e.detail.value
					var hour = time.split(':')[0];
					var min = time.split(':')[1];
					var sec = time.split(':')[2];
					this.result = Number(hour * 3600) + Number(min * 60) + Number(sec);
					console.log("时间结果为" + this.result)
				} else {
					this.result = e.detail.value
					console.log("结果为" + this.result)
				}
			},
			look(){
				uni.navigateTo({
					url:'../gonggao/gonggao?number='+3+'&index='+0
				})
			}
			// tijiao() {
			// 	wRecord({
			// 		"competitionId": this.competitionId,
			// 		"projectGradeDtos": [{
			// 			"groupType": this.groupType,
			// 			"projectId": this.projectsId,
			// 			"result": this.result
			// 		}],
			// 		"userId": this.userId
			// 	}).then((res) => {
			// 		let [err, data] = res
			// 		console.log("ok")
			// 		console.log(data)
			// 	})
			// }
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		h3 {
			font-size: 16px;
			font-weight: 900;
			padding-left: 20px;
			padding-top: 15px;
			text-align: center;
		}

		.top {
			border-bottom: 2px solid #C0C0C0;

			.info {
				display: flex;
				padding: 24px;
				.left {
					flex: 7;
					view {
						line-height: 40px;
					}
				}
				.right {
					flex: 5;
					image {
						width: 100%;
						height: 100%;
						border: 1px solid #C0C0C0;
					}
				}
			}
		}
		button {
			left: 50%;
			transform: translateX(-50%);
		}
		.outer{
			text-align: center;
			margin-bottom: 10px;
			margin-top: 20px;
			.title{
				display: inline-block;
				font-size: 14px;
				font-weight: 900;
				width: 30%;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				vertical-align: top;
			}
			.chengji{
				display: inline-block;
				vertical-align: middle;
				padding-left: 10px;
				font-size: 12px;
				border: 2px solid #ccc;
				margin-left: 10px;
				width: 55%;
			}
		}
		
	}
</style>
